package com.sinosoft.call.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sinosoft.call.entity.model.CallConfig;
import com.sinosoft.call.service.CallService;
import com.sinosoft.utils.logback.LogUtil;
import com.sinosoft.utils.request.SOAPUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.Set;

@Service("axisCallService")
public class AXISCallServiceImpl implements CallService{

	@Override
	public JSONObject callService(CallConfig callConfig, JSON param) {
		JSONObject param1 = (JSONObject) param;
		JSONObject re; 
		//请求方法 入参说明
		JSONObject menthodParam = null;
		if(StringUtils.isNotBlank(callConfig.getMenthodparam())){
			menthodParam = JSONObject.parseObject(callConfig.getMenthodparam());
			Set keys = menthodParam.keySet();
			Iterator it = keys.iterator();
			while(it.hasNext()){
				String key = it.next().toString();
				Object value = menthodParam.get(key);
				if(value!=null){
					String v = menthodParam.getString(key).trim();
					if("res".equals(v)){
						//请求报文
						String requestStr = getMassage(callConfig.getRequeststr(), param1);
						//移除注解
						value = requestStr.replaceAll("<!--.*-->", "");
						if(requestStr.length() < 8000){//请求报文过长不打印
						    LogUtil.business("请求报文:{}", requestStr);
						}
					}else if(v.startsWith("#")){
						String paramKey = v.replace("#", "");
						if(param1.containsKey(paramKey)){
							//把MenthodParam中的value　#入参中的属性#　进行替换
							value=param1.get(paramKey);
						}else{
							//把模板中的对比　#GI中的属性#　进行替换
//							if(gi!=null){
//								value = replaceParaByObject(v,gi);
//							}
						}
					}
				}
				menthodParam.put(key, value);
			}
		}
		String rsqStr = null;
			Object rsqObj = SOAPUtil.aixService1(callConfig.getNamespace(),callConfig.getCallurl(), callConfig.getMenthod(), menthodParam);
			if(rsqObj instanceof String){
				rsqStr = rsqObj.toString();
			}
		re = getFormatResponseXML(rsqStr,callConfig.getCode(),callConfig.getPrintflag());
		return re;
	}

}
